/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.leftPanels;

import controller.Data;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.JCheckBox;
import model.clustering.ClusteredDataTXTCreator;
import view.centerPanels.ClusteringPnlCenter;
import view.main.MainGUI;
import weka.clusterers.ClusterEvaluation;
import weka.clusterers.SimpleKMeans;
import weka.core.Instances;

/**
 *
 * @author Stupi
 */
public class ClusteringPnlLeft extends javax.swing.JPanel {
    private LayoutManager FlowLayout;
    

    /**
     * Creates new form ClusteringPnlLeft
     */
    public ClusteringPnlLeft() {
        initComponents();
        setSize(MainGUI.getInstance().getWidth(), MainGUI.getInstance().getHeight());
        setVisible(true);
        checkBoxes = new ArrayList<>();
        pnlAttributes.setLayout(new FlowLayout((int) LEFT_ALIGNMENT));
       
        int numOfAttributes = Data.getInstance().getInstances().numAttributes();
        for (int i = 0; i < numOfAttributes; i++) {
            String name = Data.getInstance().getInstances().attribute(i).name();
            int blank = 30 - name.length();
            for (int j = 0; j < blank; j++) {
                name = name + " ";
            }
            checkBoxes.add(new JCheckBox(name));
            pnlAttributes.add(checkBoxes.get(i));
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmbNumClasses = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        pnlAttributes = new javax.swing.JPanel();
        btnCluster = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        cmbNumClasses.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3", "4", "5", "6", "7", "8", "9", " " }));

        jLabel1.setText("Number of classes:");

        jLabel2.setText("Ignore following attributes:");

        pnlAttributes.setPreferredSize(new java.awt.Dimension(120, 100));

        btnCluster.setText("Cluster");
        btnCluster.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClusterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmbNumClasses, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
                        .addGap(55, 55, 55))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnCluster)
                            .addComponent(pnlAttributes, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbNumClasses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlAttributes, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCluster)
                .addContainerGap(86, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnClusterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClusterActionPerformed

        int numOfClusters = Integer.parseInt(cmbNumClasses.getSelectedItem().toString());
        String removeFilters = "";

        boolean isChecked = false;
        for (int i = 0; i < checkBoxes.size(); i++) {
            if (checkBoxes.get(i).isSelected()) {
                removeFilters = removeFilters + (i+1) + ",";
                isChecked = true;
            }
        }
        if (isChecked)
            removeFilters = removeFilters.substring(0, removeFilters.length() - 1);
        
        Instances instances = Data.getInstance().getInstances();
        ClusteredDataTXTCreator c = new ClusteredDataTXTCreator(instances);
        
        
        SimpleKMeans kMeans = c.createTXTfile("methods/visualization/txtFile.txt", numOfClusters, removeFilters);
        ClusterEvaluation eval = c.getEval();
        
        System.out.println("eval: " + eval);
        
        Data.getInstance().setClassified(true);
        
        MainGUI.getInstance().getPnlCenter().removeAll();
        MainGUI.getInstance().getPnlCenter().add(new ClusteringPnlCenter(kMeans, eval));
        MainGUI.getInstance().getPnlCenter().setVisible(true);
        MainGUI.getInstance().validate();
        MainGUI.getInstance().repaint();
        

    }//GEN-LAST:event_btnClusterActionPerformed

    private ArrayList<JCheckBox> checkBoxes;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCluster;
    private javax.swing.JComboBox cmbNumClasses;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel pnlAttributes;
    // End of variables declaration//GEN-END:variables
}
